<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!doctype html>
<html>
<jsp:include page="../common/import.jsp" flush="true"></jsp:include>

<body>

<jsp:include page="../common/header.jsp" flush="true"></jsp:include>

<input id="userId" value="${user.id}" hidden="true">
<input id="userType" value="${user.type}" hidden="true">
<input type="hidden" id="pageNo" value="${agents.pageNo }">
			<input type="hidden" id="pageCount" value="${agents.pageCount }">
			<input type="hidden" id="recordSize" value="${agents.recordSize}">
			<input type="hidden" id="agents" value="${agents.list}">
			
<div class="main p20">
	<div class="container clear">
        <ul class="left-nav fl pt20 w200">
        <%-- 	<c:if test="${user.roleId==0}">
        
	        <li><a class="ahover" href="${pageContext.request.contextPath}/agent/agentList.xhtml?userId=${user.id}"><span class="iconfont">&#xf014b;</span>代理商管理</a></li>
	        </c:if>
	        <c:if test="${user.roleId==1}">
		        <c:if test="${agentSelf.proxy==1}">
		        	<li><a class="ahover" href="${pageContext.request.contextPath}/agent/agentList.xhtml?userId=${user.id}"><span class="iconfont">&#xf014b;</span>代理商管理</a></li>
		        </c:if>
	        </c:if> --%>
	        <li><a class="ahover" href="${base}/agent/agent/agentList.xhtml"><span class="iconfont">&#xf014b;</span>代理商</a></li>
            <li><a href="${base}/agent/sales/salesList.xhtml"><span class="iconfont">&#xf014d;</span>业务员</a></li>
            <li><a href="${base}/agent/merchant/merchantList.xhtml"><span class="iconfont">&#xf014d;</span>商户</a></li>
        </ul>
        
        <div class="main-right fr">
        	<div class="b m20 b-radius">
            	<div class="clear bgf5 bb">
                	<h2 class="fl h30 lg30 p5 pl10">代理商</h2>
                	<a href="javascript:" onclick="toInsert()" class="fr h20 p5 w100 m5 bg1eb9ef cfff b-radius tc"><span class="iconfont f12 vm">&#xf0175;</span>&nbsp;<span class="vm">新增代理商</span></a>
                </div>
                <p class="c999 h450 tc pt100 f30" id="noListDiv" hidden="true">暂无代理商，赶快增加新代理商吧~</p>
                <!-- <div class="h30 lg30 p10 clear bb">
                	<span class="fl w100 fb">授权区域：</span>
                    <select class="h30 lg30 b w200">
                    	<option>全部</option>
                        <option>河北省</option>
                        <option>上海市</option>
                    </select>
                </div>
                
                <div class="h30 lg30 p10 clear bb">
                	<span class="fl w100 fb">级别：</span>
                    <input id="all_agent" type="radio" name="agent_level"><label for="all_agent" class="mr30">全部</label>
                    <input id="general_agent" type="radio" name="agent_level"><label for="general_agent" class="mr30">总代理</label>
                    <input id="core_agent" type="radio" name="agent_level"><label for="core_agent" class="mr30">核心代理</label>
                </div>
                
                <div class="h30 lg30 p10 clear bb">
                	<span class="fl w100 fb">代理权：</span>
                    <input id="all_proxy" type="radio" name="proxy"><label for="all_proxy" class="mr30">全部</label>
                    <input id="proxy_yes" type="radio" name="proxy"><label for="proxy_yes" class="mr30">有</label>
                    <input id="proxy_no" type="radio" name="proxy"><label for="proxy_no" class="mr30">无</label>
                </div>
            	 -->
                <table class="mt10 w bt bb" id="tab">
                	<thead>
                    	<tr>
                            <th>序号</th>
                            <th>代理商名称</th>
                            <th>联系人</th>
                            <th>联系人手机号</th>
                            <th>授权区域</th>
                            <c:if test="${user.roleId=='0'}">
                            <th>级别</th>
                            <th>有无代理权</th>
                            </c:if>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                    	<c:forEach items="${agents.list}" var="agent">
                    	<tr>
                        	 <td>${agent.id}</td> 
                            <td class="max-w200">${agent.name }</td>
                            <td>${agent.contactName}</td>
                            <td>${agent.contactPhone}</td>
                            <td>${agent.authorZone}</td>
                            <c:if test="${user.roleId=='0'}">
                            <td>
	                            <c:if test="${agent.level==1}">总代理</c:if>
	                            <c:if test="${agent.level==2}">核心代理</c:if>
	                            <c:if test="${agent.level==3}">普通代理</c:if>
                            </td>
                            <td>
                            <c:choose>
                            	<c:when test="${agent.proxy==1}">是</c:when>
                            	<c:otherwise>否</c:otherwise>
                            </c:choose>
	                            <%-- <c:if test="${agent.proxy==1}">是</c:if>
	                           <c:if test="${agent.proxy==0}">否</c:if> --%>
                            </td>
                            </c:if>
                            <td>
	                            <c:if test="${agent.status== 1}">
	                            	<button class="h20 pl5 pr5 bg1eb9ef cfff b-radius" id="userStatus" onclick="freeze(${agent.id},'0')" value="0">冻结</button>
	                            </c:if>
	                            <c:if test="${agent.status==0}">
	                            	<button class="h20 pl5 pr5 bg999 cfff b-radius" id="userStatus" onclick="freeze(${agent.id},'1')" value="1">解冻</button>
	                            </c:if>
                                <button class="h20 lg20 iconfont" onclick="edit(${agent.id})">&#xf0199;</button>
                            </td>
                        </tr>
                    	</c:forEach>
                        <!-- <tr>
                        	<td>2</td>
                            <td class="max-w200">无锡电子商务有限公司</td>
                            <td>联系人</td>
                            <td>13683036376</td>
                            <td>北京市</td>
                            <td>核心代理</td>
                            <td>是</td>
                            <td>
                            	<button class="h20 pl5 pr5 bg999 cfff b-radius">解冻</button>
                                <button class="h20 lg20 iconfont">&#xf0199;</button>
                            </td>
                        </tr> -->
                    </tbody>
                </table>
                
               <!--  <div class="clear">
                    <div class="fr p20 page">
                    	<span>1/3页</span>
                    	<a href="javascript:">下一页</a>
                        <a href="javascript:">跳转</a>
                    </div>
                </div> -->
                <div id="pagination" class="pagination fr"></div>
                
            </div>
        </div>
    </div>
</div>
<jsp:include page="../common/footer.jsp" flush="true"></jsp:include>
<script type="text/javascript">
//新增代理商
function toInsert(){
	var userId = document.getElementById("userId").value;
	window.location.href="${base}/agent/agent/toInsert.xhtml";
}
//修改代理商
function edit(agentId){
	window.location.href="${base}/agent/agent/toEdit.xhtml?agentId="+agentId;
}
//冻结/解冻
function freeze(agentId,status){
	var userType=document.getElementById("userType").value;
	var type = userType+1;
	var data = {"type":type,"agentId":agentId,"status":status};
	$.ajax({
		url: "${base}/agent/agent/freeze.xhtml",
		type: "POST",
		data: data,
		dataType: "JSON",
		success: function (data) {
        	alert(data.info);
        	/* if (data.status==1) {
				alert(data.info);
				window.location.href="${pageContext.request.contextPath}/agent/agentList.xhtml?roleId=1";
			}
        	if(data.status=='0'){
				alert(data.info);
			} */
	        location.reload(true);
        },
        error: function (data) {
            alert('服务端报错，' + data.status);
        }
	}); 
}

$(document).ready(function() {
	var agents = $("#agents").val();
	if (agents!=null&&agents!='[]') {
		//分页
		pageJs($("#pageNo").val(),$("#pageCount").val());
	}else{
		$("#pagination").attr("hidden",true);
		$("#tab").attr("hidden",true);
		$("#noListDiv").attr("hidden",false);
	}
	//分页
			
});

//-----------------------------------------第二种分页-----------------------------------------------------
function pageJs(pageNo, pageCount){
	var html = '';
	html += '<li><a onclick="topage(1)" style="cursor:pointer;">首页</a></li>';
	if(parseInt(pageNo) == 1) {
		html += '<li class="disabled"><a>&laquo;</a></li>';
	} else {
		html += '<li><a onclick="topage('+(parseInt(pageNo)-1)+')" style="cursor:pointer;">&laquo;</a></li>';
	}
	var no = 0;
    for(var i=0; i<2; i++) {
      	no = parseInt(pageNo) - (2-i);
      	if(no > 0) {
	   		html += '<li onclick="topage('+no+')" style="cursor:pointer;"><span>'+no+'</span></li>';
       	}
   	}
	html += '<li class="active" onclick="topage('+parseInt(pageNo)+')" style="cursor:pointer;"><span>'+parseInt(pageNo)+'</span></li>';
	for(var i=1; i<11; i++) {
		no = parseInt(pageNo) + i;
		if(no > parseInt(pageCount)) {
			break;
		}
		html += '<li onclick="topage('+no+')" style="cursor:pointer;"><span>'+no+'</span></li>';
	}
   	if(parseInt(pageNo) == parseInt(pageCount)) {
		html += '<li class="disabled"><a>&raquo;</a></li>';
     } else {
		html += '<li><a onclick="topage('+(parseInt(pageNo)+1)+')" style="cursor:pointer;">&raquo;</a></li>';
   	}
   	html += '<li><a onclick="topage('+parseInt(pageCount)+')" style="cursor:pointer;">末页</a></li>';
    html += '<li><a>共<span class="text-danger">'+$("#recordSize").val()+'</span>条记录,';
    html += '<span class="text-danger">'+parseInt(pageCount)+'</span>页</a></li>';
            
	$("#pagination").html(html);
}

function topage(pageNo) {
	window.location.href = "${pageContext.request.contextPath}/agent/agentList.xhtml?pageNo="+pageNo;
}


</script>

</body>
</html>